export function addWatermark(fileUrl) {
	return new Promise(async (resolve, reject) => {
		uni.getImageInfo({
			src: fileUrl,
			fail: (error) => {}, // 替换为你的图片路径
			success: async (res) => {
				// 获取文件系统管理器
				let canvasWidth = res.width;
				let canvasHeight = res.height;
				console.log(canvasWidth, canvasHeight);
				const canvas = wx.createOffscreenCanvas({
					type: '2d',
					width: canvasWidth,
					height: canvasHeight
				})
				let img = canvas.createImage(); // 注意是使用canvas实例 不是ctx
				// 等待图片加载
				await new Promise(resolve => {
					img.onload = resolve
					img.src = fileUrl; // 要加载的图片 url
				})
				canvas.width = canvasWidth;
				canvas.height = canvasHeight;
				const ctx = canvas.getContext('2d')
				ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight);
				//水印文字样式相关
				let fontSize = '20px arial'; //水印字体大小
				let maskText = '北星掼牌'; //水印文字
				let fontColor = "rgba(255,255,255,.5)"; //水印颜色
				let lineHeight = 60; //水印文字行高
				let textWidth = 180; //水印文字宽度
				let diagonalLength = canvasHeight > canvasWidth ? canvasHeight * 2 :
					canvasWidth; //选取最长边
				// 保证水印文字范围是矩形最长边的2倍
				ctx.translate(canvasWidth / 2, -diagonalLength);
				ctx.rotate(45 * Math.PI / 180); //设置文字的旋转角度，角度为45°；
				// 横向循环次数
				let crossTime = Math.ceil(diagonalLength * 2 / textWidth);
				// 竖向循环次数
				let verticalTime = Math.ceil(diagonalLength * 2 / lineHeight);
				for (let j = 0; j < verticalTime; j++) { //纵向循环
					ctx.font = fontSize;
					ctx.fillStyle = fontColor;
					ctx.fillText(maskText, 0, lineHeight * j);
					for (let i = 1; i < crossTime; i++) { //横向循环
						ctx.font = fontSize;
						ctx.fillStyle = fontColor;
						ctx.fillText(maskText, textWidth * i, lineHeight * j);
					}
				};
				uni.canvasToTempFilePath({
					width: canvasWidth,
					height: canvasHeight,
					// canvasId: canvasId,
					canvas: canvas,
					// fileType: "jpg",
					success: (res) => {
						resolve(res.tempFilePath)
					},
					fail: (error) => {
						reject(error)
						console.error('Failed to save canvas:', error);
					},
				});
			},
			flail: (error) => {
				reject(error)
			}
		});

	});
}